//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var circleUrl = require("url/circle");
var httpSilence = require("helper/httpSilence");
var hashdata = mm("do_HashData");
//声明UI变量
var listview=ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var httpUrl = circleUrl.url.member.myUnPrivateCircles;//我的圈子（未隐藏）
/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

//右上角-确定
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "确定";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	var selectCircles = [];
	for(var i=0;i<listdata.getCount();i++){
		oneData = listdata.getOne(i);
		if(oneData.checked){
			selectCircles.push(oneData);
		}
	}
	app.closePage(selectCircles);
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "选择我的圈子"
})
header.refreshData();

/****************处理页面-头部-end*****************/
//andriod返回键
page.on("back", function() {
	app.closePage();
});

page.on("loaded", function() {
	loadData();
});


//更改选中状态
page.on("check_change_event", function(d) {
	var oneData = null;
	for(var i=0;i<listdata.getCount();i++){
		oneData = listdata.getOne(i);
		if(oneData.id == d.id){
			oneData.checked = d.checked;
			listdata.updateOne(i, oneData);//全部设为“不选中”
		}
	}
	//listview.refreshItems();
})

function loadData(){
	httpSilence.post(httpUrl,{},function(data){
		if(data.circleList){
			listdata.removeAll();
			listdata.addData(data.circleList);
			for(var i=0;i<listdata.getCount();i++){
				var oneData = listdata.getOne(i);
				oneData.checked = false;
				listdata.updateOne(i, oneData);//全部设为“不选中”
			}
			listview.refreshItems();
		}
	});
}
